##
# MegaZeux Makefile fragment
##

include contrib/gdm2s3m/src/Makefile.in
include contrib/icons/Makefile.in
include contrib/libmodplug/src/Makefile.in

.PHONY: mzx_clean core_target_clean

LINK_CC := ${CC}

core_src = src
core_obj = src/.build

network_src = src/network
network_obj = src/network/.build

core_flags   += -ffast-math -funsigned-char
core_flags   += -Wmissing-format-attribute -Wpointer-arith
core_flags   += -Wwrite-strings
core_cflags  += ${CFLAGS} -Wmissing-prototypes
core_cxxflags = ${CXXFLAGS} -DHAVE_INTTYPES_H

core_flags   += -I${core_src} -I${PREFIX}/include
core_ldflags := -lm ${core_ldflags}

ifeq (${PLATFORM},mingw)
core_spec := -DCORE_LIBSPEC="__declspec(dllexport)"
endif

${core_obj}/main.o: ${core_src}/main.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${core_cflags} ${core_flags} -c $< -o $@

${core_obj}/run_stubs.o: ${core_src}/run_stubs.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${core_cflags} ${core_flags} -c $< -o $@

${core_obj}/updater.o: ${core_src}/updater.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${core_cflags} ${core_flags} -c $< -o $@

${network_obj}/%.o: ${network_src}/%.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${core_cflags} ${core_flags} ${core_spec} -c $< -o $@

${core_obj}/%.o: ${core_src}/%.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${core_cflags} ${core_flags} ${core_spec} -c $< -o $@

${core_obj}/%.o: ${core_src}/%.cpp
	$(if ${V},,@echo "  CXX     " $<)
	${CXX} -MD ${core_cxxflags} ${core_flags} ${core_spec} -c $< -o $@

#
# Lists mandatory C language sources (mangled to object names) required
# to build the main binary. Please keep this sorted alphabetically.
#
core_cobjs := \
  ${core_obj}/board.o ${core_obj}/configure.o ${core_obj}/counter.o  \
  ${core_obj}/data.o ${core_obj}/error.o ${core_obj}/event.o         \
  ${core_obj}/expr.o ${core_obj}/fsafeopen.o ${core_obj}/game2.o     \
  ${core_obj}/game.o ${core_obj}/graphics.o ${core_obj}/idarray.o    \
  ${core_obj}/idput.o ${core_obj}/intake.o ${core_obj}/legacy_rasm.o \
  ${core_obj}/mzm.o ${core_obj}/render.o ${core_obj}/robot.o         \
  ${core_obj}/run_robot.o ${core_obj}/scrdisp.o ${core_obj}/sfx.o    \
  ${core_obj}/sprite.o ${core_obj}/util.o ${core_obj}/validation.o   \
  ${core_obj}/window.o ${core_obj}/world.o

#
# Lists mandatory C++ language sources (mangled to object names) required
# to build the main binary. Currently there are only optional sources.
#
core_cxxobjs :=

#
# Hack to avoid silly warnings from devkitPro headers
#
ifeq ($(or ${BUILD_WII},${BUILD_NDS},${BUILD_PSP}),)
core_cflags += -Wstrict-prototypes
endif

#
# Hack to avoid silly warnings from Android headers
#
ifeq (${PLATFORM},android)
core_cflags += -Wno-strict-prototypes
endif

#
# These are really just hacks and should be moved elsewhere
#
ifeq (${BUILD_NDS},1)
core_cobjs += arch/nds/ram.o arch/nds/exception.o
core_cobjs += arch/nds/dlmalloc.o
core_cobjs += arch/nds/platform.o arch/nds/event.o
core_cobjs += arch/nds/render.o arch/nds/evq.o
ifneq (${BUILD_DEBYTECODE},1)
core_cobjs += arch/nds/extmem.o
endif
endif

ifeq (${PLATFORM},android)
core_cxxobjs += arch/android/sfwrapper.o
LINK_CC := ${CXX}
endif

ifeq (${BUILD_WII},1)
core_cobjs += arch/wii/event.o arch/wii/platform.o
ifeq (${BUILD_AUDIO},1)
core_cobjs += arch/wii/audio.o
endif
endif

#
# Other modular stuff
#

ifeq (${BUILD_HELPSYS},1)
core_cobjs += ${core_obj}/helpsys.o
endif

ifeq (${BUILD_RENDER_SOFT},1)
core_cobjs += ${core_obj}/render_soft.o
endif

# GL shared
ifneq ($(or ${BUILD_RENDER_GL_FIXED},${BUILD_RENDER_GL_PROGRAM}),)
core_cobjs += ${core_obj}/render_gl.o
endif

# Fixed function renderers (OGL=1, OGLES=1)
ifeq (${BUILD_RENDER_GL_FIXED},1)
core_cobjs += ${core_obj}/render_gl1.o ${core_obj}/render_gl2.o
endif

# Shader renderers (OGL>=2, OGLES=2)
ifeq (${BUILD_RENDER_GL_PROGRAM},1)
core_cobjs += ${core_obj}/render_glsl.o
endif

#
# Currently only usable with GL, but could theoretically be used
# with OpenVG in the future.
#
ifeq (${BUILD_EGL},1)
core_cobjs += ${core_obj}/render_egl.o
core_ldflags := -lEGL ${core_ldflags}
ifneq (${X11DIR},)
core_flags += -I${X11DIR}/../include
core_ldflags := -L${X11DIR}/../${RAWLIBDIR} -lX11 ${core_ldflags}
endif
endif

ifeq (${BUILD_RENDER_YUV},1)
core_cobjs += ${core_obj}/render_yuv.o
core_cobjs += ${core_obj}/render_yuv1.o ${core_obj}/render_yuv2.o
endif

ifeq (${BUILD_RENDER_GP2X},1)
core_cobjs += ${core_obj}/render_gp2x.o
endif

ifeq (${BUILD_RENDER_GX},1)
core_cobjs += ${core_obj}/render_gx.o
endif

ifeq (${BUILD_MODPLUG},1)
core_cflags += -I${gdm2s3m_src}
core_cobjs += ${gdm2s3m_objs}
core_flags += -I${libmodplug_src} -I${libmodplug_src}/libmodplug
core_cxxobjs += ${core_obj}/audio_modplug.o ${libmodplug_objs}
LINK_CC := ${CXX}
endif

ifeq (${BUILD_MIKMOD},1)
core_ldflags := ${MIKMOD_LDFLAGS} ${core_ldflags}
core_cobjs += ${core_obj}/audio_mikmod.o
endif

ifeq (${LIBPNG},1)
core_flags += ${LIBPNG_CFLAGS} -DNEED_PNG_WRITE_SCREEN
core_ldflags := ${LIBPNG_LDFLAGS} ${core_ldflags}
core_cobjs += ${core_obj}/pngops.o
endif

ifeq (${BUILD_AUDIO},1)
core_cobjs += ${core_obj}/audio.o
core_flags += ${VORBIS_CFLAGS}
core_ldflags := ${VORBIS_LDFLAGS} ${core_ldflags}
endif

ifeq (${BUILD_SDL},1)

core_cobjs += ${core_obj}/event_sdl.o ${core_obj}/platform_sdl.o
core_cobjs += ${core_obj}/render_sdl.o

ifeq (${BUILD_AUDIO},1)
core_cobjs += ${core_obj}/audio_sdl.o
endif

core_flags   += ${SDL_CFLAGS}
core_ldflags += ${SDL_LDFLAGS}

ifeq (${BUILD_MODULAR},1)
mzx_ldflags    += ${SDL_LDFLAGS}
mzxrun_ldflags += ${SDL_LDFLAGS}
endif

else

ifeq ($(or ${BUILD_WII},${BUILD_NDS}),)

core_cobjs += ${core_obj}/platform_dummy.o

#
# Normally SDL would provide us with pthread, but in the case
# we're not using SDL or any other platform mutex implementation,
# we must link pthread ourselves.
#
ifeq (${PTHREAD},1)
core_ldflags := ${PTHREAD_LDFLAGS} ${core_ldflags}
endif

endif

endif

ifeq (${BUILD_NETWORK},1)

core_cflags += ${ZLIB_CFLAGS}
core_ldflags := ${ZLIB_LDFLAGS} ${core_ldflags} 

core_cobjs += ${network_obj}/host.o ${network_obj}/manifest.o
core_cobjs += ${network_obj}/network.o ${network_obj}/sha256.o

ifeq (${BUILD_UPDATER},1)
mzx_objs += ${core_obj}/updater.o
endif

ifeq (${PLATFORM},amiga)
core_ldflags := ${core_ldflags} -lnet
endif

endif

ifeq (${BUILD_DEBYTECODE},1)
core_cobjs += ${core_obj}/rasm.o
endif

core_objs := ${core_cobjs} ${core_cxxobjs}

-include ${core_objs:.o=.d}

${core_objs}: $(filter-out $(wildcard ${core_obj}), ${core_obj})
${core_objs}: $(filter-out $(wildcard ${network_obj}), ${network_obj})

ifeq (${BUILD_MODULAR},1)

core_target := ${DSOPRE}core${DSOPOST}

${core_target}: ${core_objs}
	$(if ${V},,@echo "  LINK    " $@)
	${LINK_CC} ${DSOLDFLAGS} -o $@ ${core_objs} ${LDFLAGS} \
	  ${DSOSONAME}$@ ${core_ldflags}

core_target_clean:
	$(if ${V},,@echo "  RM      " ${core_target} ${core_target}.debug)
	${RM} ${core_target} ${core_target}.debug

else

core_target := ${core_objs}

core_target_clean:

endif

ifeq (${BUILD_EDITOR},1)

include src/editor/Makefile.in

mzx_objs := ${mzx_objs} ${core_obj}/main.o

ifeq (${PLATFORM},darwin)
mzx_objs += arch/darwin/SDLMain.o
mzx_ldflags += -framework Cocoa
endif

-include ${mzx_objs:.o=.d}

ifeq (${EMBED_ICONS},1)
mzx_objs += ${icons}
endif

ifeq (${BUILD_MODULAR},1)
${mzx}.debug: ${core_target}.debug ${editor_target}.debug
endif

${mzx}: ${core_target} ${editor_target} ${mzx_objs}
	$(if ${V},,@echo "  LINK    " ${mzx})
ifeq (${BUILD_MODULAR},1)
	${CC} ${mzx_objs} -o ${mzx} ${ARCH_EXE_LDFLAGS} ${LDFLAGS} \
	  ${DSORPATH} -L. -lcore -leditor \
	  ${mzx_ldflags}
else
	${LINK_CC} ${mzx_objs} ${core_target} ${editor_target} \
	  -o ${mzx} ${ARCH_EXE_LDFLAGS} ${LDFLAGS} \
	  ${core_ldflags} ${editor_ldflags}
endif

mzx_clean: editor_clean

else

.PHONY: ${mzx}.debug
${mzx}:
${mzx}.debug:

endif

ifeq (${BUILD_MZXRUN},1)

mzxrun_objs := ${core_obj}/main.o ${core_obj}/run_stubs.o

ifeq (${PLATFORM},darwin)
mzxrun_objs += arch/darwin/SDLMain.o
mzxrun_ldflags += -framework Cocoa
endif

-include ${mzxrun_objs:.o=.d}

ifeq (${EMBED_ICONS},1)
mzxrun_objs += ${icons}
endif

ifeq (${BUILD_MODULAR},1)
${mzxrun}.debug: ${core_target}.debug
endif

${mzxrun}: ${core_target} ${mzxrun_objs}
	$(if ${V},,@echo "  LINK    " ${mzxrun})
ifeq (${BUILD_MODULAR},1)
	${CC} ${mzxrun_objs} -o ${mzxrun} ${ARCH_EXE_LDFLAGS} ${LDFLAGS} \
	  ${DSORPATH} -L. -lcore ${mzxrun_ldflags}
else
	${LINK_CC} ${mzxrun_objs} ${core_target} \
	  -o ${mzxrun} ${ARCH_EXE_LDFLAGS} ${LDFLAGS} ${core_ldflags}
endif

else

.PHONY: ${mzxrun}.debug
${mzxrun}:
${mzxrun}.debug:

endif

mzx_clean: core_target_clean gdm2s3m_clean icons_clean libmodplug_clean
	$(if ${V},,@echo "  RM      " ${core_obj} ${network_obj})
	${RM} -r ${core_obj} ${network_obj}
	$(if ${V},,@echo "  RM      " ${mzxrun} ${mzxrun}.debug)
	${RM} ${mzxrun} ${mzxrun}.debug
	$(if ${V},,@echo "  RM      " ${mzx} ${mzx}.debug)
	${RM} ${mzx} ${mzx}.debug
